﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Vmago.Models.DBML;
using Vmago;
using System.Xml;
using System.Collections;
using System.Web.UI;
using System.Reflection;
using System.Text;


namespace Vmago.Models.DAL
{
    public class ComDAL
    {
        #region Global：全局变量

        private TblDataContext m_dcTbl = new TblDataContext();

        private List<tbl_shopCategory> m_loCategory = new List<tbl_shopCategory>();

        #endregion

        #region DropDownList：Tbl_ShopCategory

        public SelectList GetShopCategory(String ClassID)
        {
            Int32 intClassID =0;
            Int32.TryParse(ClassID,out intClassID);

            IEnumerable<tbl_shopCategory> oShopCategory = GetShopCategory();

            if (intClassID == 0)
                return new SelectList(oShopCategory, "categoryID", "categoryName");
            else
                return new SelectList(oShopCategory.Where(o => o.classID == intClassID), "categoryID", "categoryName");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<tbl_shopCategory> GetShopCategory()
        {
            m_loCategory = new List<tbl_shopCategory>();

            GetShopCategory_Loop(0, 0);

            return m_loCategory.AsEnumerable();
        }

        /// <summary>
        /// 树形结构——关于
        /// </summary>
        /// <param name="oParentID"></param>
        private void GetShopCategory_Loop(int intParentID, int intLevel)
        {
            IEnumerable<tbl_shopCategory> oChildern = from p in m_dcTbl.tbl_shopCategory
                                                      where p.parentID.Equals(intParentID)
                                                      orderby p.categoryID ascending
                                                      select p;

            foreach (tbl_shopCategory oChid in oChildern)
            {
                String indent = "　　";
                String head = "●";
                StringBuilder level_name = new StringBuilder(oChid.categoryName);

                oChid.categoryName = level_name.Insert(0, head).Insert(0, indent, intLevel).ToString();
                m_loCategory.Add(oChid);

                GetShopCategory_Loop(oChid.categoryID, intLevel + 1);
            }
        }

        #endregion

        #region DropDownList：Tbl_Class

        /// <summary>查询大分类</summary>
        public SelectList GetClass()
        {
            IEnumerable<tbl_class> oClass = from p in m_dcTbl.tbl_class
                                            select p;
            return new SelectList(oClass, "classID", "className");
        }

        #endregion

        #region DropDownList：Tbl_GeoCountry

        /// <summary>查询国家</summary>
        public SelectList GetGeoCountry()
        {
            IEnumerable<tbl_geoCountry> oCountry = from p in m_dcTbl.tbl_geoCountry
                                                   select p;

            return new SelectList(oCountry, "countryID", "countryName");
        }

        #endregion

        #region DropDownList：Tbl_GeoProvince

        /// <summary>查询地区</summary>
        public SelectList GetGeoProvince()
        {
            IEnumerable<tbl_geoProvince> oProvince = from p in m_dcTbl.tbl_geoProvince
                                                     select p;

            return new SelectList(oProvince, "provinceID", "provinceName");
        }

        /// <summary>查询地区</summary>
        public SelectList GetGeoProvince(String CountryID)
        {
            Int32 intCountryID = 0;
            Int32.TryParse(CountryID, out intCountryID);

            IEnumerable<tbl_geoProvince> oProvince = from p in m_dcTbl.tbl_geoProvince
                                                     where p.countryID == intCountryID
                                                     select p;

            return new SelectList(oProvince, "provinceID", "provinceName");
        }

        #endregion

        #region DropDownList：Tbl_GeoCty

        /// <summary>查询城市</summary>
        public SelectList GetGeoCity()
        {
            IEnumerable<tbl_geoCity> oCity = from p in m_dcTbl.tbl_geoCity
                                             select p;

            return new SelectList(oCity,"cityID","cityName");
        }

        /// <summary>查询城市</summary>
        public SelectList GetGeoCity(String ProvinceID)
        {
            Int32 intProvinceID = 0;
            Int32.TryParse(ProvinceID,out intProvinceID);

            IEnumerable<tbl_geoCity> oCity = from p in m_dcTbl.tbl_geoCity
                                             where p.provinceID == intProvinceID
                                             select p;

            return new SelectList(oCity, "cityID", "cityName");
        }

        #endregion

        #region DropDownList：Tbl_GeoSuburb

        /// <summary>查询区域</summary>
        public SelectList GetGeoSuburb()
        {
            IEnumerable<tbl_geoSuburb> oSuburb = from p in m_dcTbl.tbl_geoSuburb
                                                 select p;

            return new SelectList(oSuburb,"suburbID","suburbName");
        }

        /// <summary>查询区域</summary>
        public SelectList GetGeoSuburb(String CityID)
        {
            Int32 intCityID = 0;
            Int32.TryParse(CityID, out intCityID);

            IEnumerable<tbl_geoSuburb> oSuburb = from p in m_dcTbl.tbl_geoSuburb
                                                 where p.cityID == intCityID
                                                 select p;

            return new SelectList(oSuburb, "suburbID", "suburbName");
        }

        #endregion        
    }
}